﻿Imports BLL
Imports DAL
Class EletronicosCAD
    Inherits System.Web.UI.Page
    Dim objbll As New EletronicosBLL
    Dim cod As Integer = 0
#Region "Eventos"
#Region "Botão Gravar"
    Protected Sub BGravar_Click(sender As Object, e As EventArgs) Handles BGravar.Click
        Try
            objbll = New EletronicosBLL(Val(TCodigo.Text), TDescricao.Text.Trim, DDLMarca.SelectedValue, DDLSubCategoria.SelectedValue, Decimal.Parse(tpeso.text))
            If (objbll.InserirAlterar(Session("usu_nome"))) Then
                BGravar.Enabled = False
                LMSG.Text = "Gravado com Sucesso!"
                LMSG.Visible = True
            End If
        Catch ex As Exception
            LMSG.ForeColor = Drawing.Color.Red
            LMSG.Text = ex.Message
            LMSG.Visible = True
        End Try
    End Sub
#End Region

#Region "Load"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objconexao As New Conexao
        
        cod = 0
        If (Not IsPostBack()) Then
            objconexao.preencheCombo(DDLMarca, "Mul_Marca", "mar_codigo", "mar_descricao", "", "mar_descricao")
            objconexao.preencheCombo(ddlcategoria, "mul_categoria", "cat_codigo", "cat_descricao", "", "cat_descricao")
            ddlcategoria_SelectedIndexChanged(Nothing, Nothing)
            If Not IsNothing(Request("id")) Then
                cod = Val(Request("id"))
                BGravar.Text = "Alterar"

                carrega()
            End If
        End If

    End Sub
#End Region

#Region "DDLCategoria Selectedindex"
    Protected Sub ddlcategoria_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlcategoria.SelectedIndexChanged
        Dim conexao As New Conexao
        conexao.preencheCombo(DDLSubCategoria, "Mul_SubCategoria", "sbc_codigo", "sbc_descricao", "cat_codigo=" & ddlcategoria.SelectedValue, "sbc_descricao")
    End Sub
#End Region
#End Region

#Region "Métodos"
    Public Sub carrega()
        objbll = New EletronicosBLL(Val(cod))
        Dim objsbc As New SubCategoriaBLL(Val(objbll.SubCodigo))
        TCodigo.Text = objbll.Codigo
        TDescricao.Text = objbll.Descricao
        ddlcategoria.SelectedValue = objsbc.CatCodigo
        ddlcategoria_SelectedIndexChanged(Nothing, Nothing)
        DDLMarca.SelectedValue = objbll.MarCodigo
        DDLSubCategoria.SelectedValue = objbll.SubCodigo
        tpeso.Text = objbll.peso
    End Sub
#End Region


    Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim objconexao As New Conexao
        objconexao.preencheCombo(ddlcategoria, "mul_categoria", "cat_codigo", "cat_descricao", "", "cat_descricao")
    End Sub

    Protected Sub ImageButton2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
        Dim objconexao As New Conexao
        objconexao.preencheCombo(DDLMarca, "Mul_Marca", "mar_codigo", "mar_descricao", "", "mar_descricao")
    End Sub
End Class